﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Configuration;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Net.Http.Json;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Todo.Helpers;
using Todo.Models;
using TodoAPI.Models;
using TodoAPI.Models.DTO;

namespace Todo.Forms
{
    public partial class UserList : Form
    {
        private int pageIndex = 1;
        private int pageSize = 10;
        private int totalPage = 0;
        private readonly HttpClient client;
        public UserList()
        {
            InitializeComponent();
            client = HttpClientHelper.CreateHttpClient(LoginInfo.CurrentUser?.Token!);
        }

        private void UserList_Load(object sender, EventArgs e)
        {
            BindHelper.BindStatus(cbbStatus);
            cbbPageSize.SelectedIndexChanged -= cbbPageSize_SelectedIndexChanged!;
            BindHelper.BindPageSize(cbbPageSize);
            cbbPageSize.SelectedIndexChanged += cbbPageSize_SelectedIndexChanged!;
            cbbPageSize_SelectedIndexChanged(sender, e);
        }

        public async void BindDataGridView()
        {
            var url = $"/User/Get?PageIndex={pageIndex}&PageSize={pageSize}";
            var result = await client.GetAsync(url);
            result.EnsureSuccessStatusCode();
            var listResult = await result.Content.ReadFromJsonAsync<ResponseListResult<ViewUser>>();
            dataGridView1.DataSource = listResult!.Data;
            totalPage = listResult!.TotalPage;
        }

        private void btnSearch_Click(object sender, EventArgs e)
        {
            pageIndex = 1;
            BindDataGridView();
        }

        private void btnFirst_Click(object sender, EventArgs e)
        {
            pageIndex = 1;
            BindDataGridView();
        }

        private void btnPrev_Click(object sender, EventArgs e)
        {
            if (pageIndex > 1)
            {
                pageIndex = pageIndex - 1;
                BindDataGridView();
            }
        }

        private void btnNext_Click(object sender, EventArgs e)
        {
            if (pageIndex < totalPage)
            {
                pageIndex = pageIndex + 1;
                BindDataGridView();
            }
        }

        private void btnLast_Click(object sender, EventArgs e)
        {
            pageIndex = totalPage;
            BindDataGridView();
        }

        private void btnGo_Click(object sender, EventArgs e)
        {
            pageIndex = int.Parse(txtPage.Text);
            BindDataGridView();
        }

        private void dtpCreateTimeEnd_ValueChanged(object sender, EventArgs e)
        {
            dtpCreateTimeEnd.Format = DateTimePickerFormat.Long;
            dtpCreateTimeEnd.CustomFormat = "";
        }

        private void dtpCreateTimeStart_ValueChanged(object sender, EventArgs e)
        {
            dtpCreateTimeStart.Format = DateTimePickerFormat.Long;
            dtpCreateTimeStart.CustomFormat = "";
        }

        private void btnClearStart_Click(object sender, EventArgs e)
        {
            dtpCreateTimeStart.Format = DateTimePickerFormat.Custom;
            dtpCreateTimeStart.CustomFormat = " ";
        }

        private void btnClearEnd_Click(object sender, EventArgs e)
        {
            dtpCreateTimeEnd.Format = DateTimePickerFormat.Custom;
            dtpCreateTimeEnd.CustomFormat = " ";
        }

        private void txtPage_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (!(char.IsDigit(e.KeyChar) || e.KeyChar == (char)Keys.Back))
            {
                e.Handled = true;
            }
        }

        private void cbbPageSize_SelectedIndexChanged(object sender, EventArgs e)
        {
            var selectedPageSize = cbbPageSize.SelectedItem as PageSize;
            pageSize = selectedPageSize!.Value;
            pageIndex = 1;
            BindDataGridView();
        }
    }
}
